package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Update extends JInternalFrame {
	Connection conn;
	
	Select ss;
	Update d;
	
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	public JTextField code;
	public JTextField ea;
	private JButton btnNewButton;
	private JButton button;
	private JLabel status;
	public JTextField serial;
	private JLabel lblSerial;
	private JButton button_1;
	
	
	
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Update frame = new Update();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Update(Connection c) {
		super("수정",true,true,true,true);
		setBounds(100, 100, 355, 218);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getButton());
		getContentPane().add(getStatus());
		getContentPane().add(getSerial());
		getContentPane().add(getLblSerial());
		getContentPane().add(getButton_1());
		
		conn=c;

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setBounds(12, 53, 41, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("ea");
			lblNewLabel_1.setBounds(12, 83, 41, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(65, 49, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(65, 80, 116, 21);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					int s=Integer.parseInt(serial.getText());
					String sql="select * from ptbl where serial=?";
					
					try{
						PreparedStatement pstmt=conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						
						ResultSet rs=pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText(""+rs.getInt("ea"));
						}else{
							status.setText("데이터가 없습니다.");
							JOptionPane.showMessageDialog(null,"업음");
							code.setText("");
							ea.setText("");
							serial.requestFocus();
						}
						
						rs.close();
						pstmt.close();
						
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(206, 17, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("수정");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Modify();
					ss.columnadd();//수정시 select의 테이블 값변경
				}
			});
			button.setBounds(65, 111, 97, 23);
		}
		return button;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력하세요");
			status.setBounds(12, 164, 246, 15);
		}
		return status;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(65, 17, 116, 21);
		}
		return serial;
	}
	private JLabel getLblSerial() {
		if (lblSerial == null) {
			lblSerial = new JLabel("serial");
			lblSerial.setBounds(12, 21, 41, 15);
		}
		return lblSerial;
	}
	
	public void setD(Select s){
		this.ss=s;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("삭제");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					del();
					ss.columnadd();
				}
			});
			button_1.setBounds(195, 111, 97, 23);
		}
		return button_1;
	}
	public void search1(){ //데이터 베이스에서 값들을 가져오는 것
		int s=Integer.parseInt(serial.getText());
		String c=code.getText();
		int e=Integer.parseInt(ea.getText());
		
		String sql="update ptbl set code=?, ea=?" + "where serial=?";
		
		try{
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, c);
			pstmt.setInt(2, e);
			pstmt.setInt(3, s);
			
			pstmt.executeUpdate();
			pstmt.close();
					
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		ss.columnadd();
	}
	public void del(){ //값을 삭제하는 것
		int sa=Integer.parseInt(serial.getText());
		String sql="delete from ptbl where serial=?";
		try{
			PreparedStatement pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, sa);
			
			pstmt.executeUpdate();
			pstmt.close();
			serial.setText("");
			code.setText("");
			ea.setText("");
			
		}catch(Exception ex){
			ex.getMessage();
		}
		ss.columnadd(); 
	}
	
	public void Modify(){
		int s= Integer.parseInt(serial.getText());
		String c=code.getText();
		int e=Integer.parseInt(ea.getText());
		
		String sql= "update ptbl set code=?, ea=?  where serial=?";
		try{
			PreparedStatement pstmt= conn.prepareStatement(sql);
			pstmt.setString(1, c);
			pstmt.setInt(2, e);
			pstmt.setInt(3, s);
			
			pstmt.executeUpdate();
			pstmt.close();
		}catch(Exception ex){
			
		}
	}
}
